/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using EnvDTE;

namespace DaveSexton.DocProject.Options
{
	public partial class OptionsDialogBase : UserControl, IDTToolsOptionsPage
	{
		#region Constructors
		public OptionsDialogBase()
		{
			InitializeComponent();
		}
		#endregion

		#region Methods
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
		private void TryResizeToContainer()
		{
			try
			{
				System.Diagnostics.Process vs = System.Diagnostics.Process.GetCurrentProcess();

				IntPtr options = NativeMethods.FindWindowEx(vs.MainWindowHandle, IntPtr.Zero, "#32770", "Options");

				if (options == IntPtr.Zero)
					// try desktop as parent
					options = NativeMethods.FindWindowEx(IntPtr.Zero, IntPtr.Zero, "#32770", "Options");

				if (options != IntPtr.Zero)
				{
					IntPtr container = NativeMethods.FindWindowEx(options, IntPtr.Zero, "#32770", "");

					if (container != IntPtr.Zero)
					{
						Rectangle bounds = NativeMethods.GetWindowRectangle(container);

						if (bounds.Width > 0 && bounds.Height > 0)
							this.Size = bounds.Size;
					}
				}
			}
			catch { }
		}

		[CLSCompliant(false)]
		protected virtual void Initialize(DTE dte)
		{
		}

		[CLSCompliant(false)]
		protected virtual void OnOk()
		{
		}

		protected virtual void OnCancel()
		{
		}

		protected virtual void OnHelp()
		{
		}

		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
		protected virtual object GetProperties()
		{
			return null;
		}
		#endregion

		#region IDTToolsOptionsPage Members
		void IDTToolsOptionsPage.GetProperties(ref object PropertiesObject)
		{
			PropertiesObject = GetProperties();
		}

		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")]
		void IDTToolsOptionsPage.OnAfterCreated(DTE DTEObject)
		{
			this.SuspendLayout();

			TryResizeToContainer();

			Initialize(DTEObject);

			this.ResumeLayout(true);
		}

		void IDTToolsOptionsPage.OnCancel()
		{
			OnCancel();
		}

		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")]
		void IDTToolsOptionsPage.OnOK()
		{
			OnOk();
		}

		void IDTToolsOptionsPage.OnHelp()
		{
			OnHelp();
		}
		#endregion
	}
}
